<?php

class Test_Abovo_Model_Perms extends Solar_Test {
    
    private $_perms;
    
    public function setup()
    {
        $this->_perms = Solar::factory('Abovo_Model_Perms');
    }
    
    public function testGrant()
    {
        $this->assertTrue($this->_perms->grant('bauer', 'Abovo_App_Crm_Companies', 'add'));
        
        $this->assertTrue(count($this->_perms->fetchAll()) === 1);
    }
    
    public function testFetchAllByHandle()
    {
        $this->_perms->grant('jack', 'Abovo_App_Crm_Companies', 'add');
        $this->_perms->grant('bauer', 'Abovo_App_Crm_Contacts', 'add');
        $this->_perms->grant('jack', 'Abovo_App_Crm_Contacts', 'add');
        
        $perms = array(
            array(
                'action' => 'add',
                'class'  => 'Abovo_App_Crm_Companies',
            ),
            array(
                'action' => 'add',
                'class'  => 'Abovo_App_Crm_Contacts',
            ),
        );
        
        $this->assertSame($this->_perms->fetchAllByHandle('jack'), $perms);
    }
    
    public function testRefresh()
    {
        $this->_perms->grant('bauer', 'Abovo_App_Crm_Contacts', 'search');
        $this->_perms->grant('bauer', 'Abovo_App_Crm_Contacts', 'show');
        $this->_perms->grant('bauer', 'Abovo_App_Example_App', 'something');
        
        // second set of permissions
        $perms = array(
            array(
                'class'  => 'Abovo_App_Crm_Contacts',
                'action' => 'edit',
            ),
            array(
                'class'  => 'Abovo_App_System_Prefs',
                'action' => 'change',
            )
        );
        
        $this->_perms->refresh('bauer', $perms);
        
        $out = $this->_perms->fetchAllByHandle('bauer');
        
        $this->assertSame($out, $perms);
    }
    
    public function tearDown()
    {
        Solar::registry('sql')->query("DELETE FROM {$this->_perms->name}");
    }
    
}

